﻿using BLL;
using Model;
using System;
using Thksoft.Modal.Page;

namespace Thksoft.Modal
{
    public partial class EditCustomer : FormPage
    {
        private Sys_CompanyBLL companyBLL = new Sys_CompanyBLL();
        private Yx_CustomerBLL customerBLL = new Yx_CustomerBLL();

        protected override void BindPage()
        {
            if (FORM_IFEDIT)
            {
                var entity = customerBLL.Find(FORM_KEY);
                FORM_NAME.Text = entity.C_NAME;

                C_REAL_NAME.Text = entity.C_REAL_NAME;
                FORM_PHONE.Text = entity.C_PHONE;
                FORM_ADDRESS.Text = entity.C_ADDRESS;
                FORM_REMARK.Text = entity.C_REMARK;

                FORM_STATE.SelectedValue = entity.N_STATE.ToString();
                FORM_COMPANY_ID.Value = entity.N_COMPANY_ID;
                FORM_CUSTOMER_TYPE.SelectedValue = entity.N_CUSTOMER_TYPE == null ? "-1" : entity.N_CUSTOMER_TYPE.ToString();
            }
        }

        protected override void SaveClick(object sender, EventArgs e)
        {
            if (FORM_NAME.Text.TrimEnd() == "")
            {
                MessageBox.Show(this, "客户公司名称不能为空");
                return;
            }

            if (FORM_COMPANY_ID.Value == -1)
            {
                MessageBox.Show(this, "请选择公司");
                return;
            }
            //if (FORM_PHONE.Text.TrimEnd() == "")
            //{
            //    MessageBox.Show(this, "联系号码不能为空");
            //    return;
            //}
            //if (FORM_ADDRESS.Text.TrimEnd()=="")
            //{
            //    MessageBox.Show(this,"客户公司地址不能为空");
            //    return;
            //}
            /*int namelength = FORM_NAME.Text.Length;
            if (namelength>25)
            {
                MessageBox.Show(this, "客户公司名称长度不能大于25位");
                return;
            }*/
            if (FORM_IFEDIT)
            {
                var entity = customerBLL.Find(FORM_KEY);
                if (entity.C_NAME != FORM_NAME.Text.TrimEnd())
                {
                    var sql = "SELECT COUNT(*) FROM YX_CUSTOMER WHERE N_COMPANY_ID = " + FORM_COMPANY_ID.Value + " AND C_NAME = '" + FORM_NAME.Text.TrimEnd() + "'";
                    if (customerBLL.GetCount(sql) > 0)
                    {
                        MessageBox.Show(this, "此客户公司信息已添加！");
                        return;
                    }
                }

                entity.C_ADDRESS = FORM_ADDRESS.Text.TrimEnd();

                entity.C_NAME = FORM_NAME.Text.TrimEnd();
                entity.C_REAL_NAME = C_REAL_NAME.Text;
                entity.C_PHONE = FORM_PHONE.Text.Trim();
                entity.N_COMPANY_ID = FORM_COMPANY_ID.Value;
                entity.N_STATE = Convert.ToDecimal(FORM_STATE.SelectedValue);
                entity.C_REMARK = FORM_REMARK.Text;
                if (FORM_CUSTOMER_TYPE.SelectedValue == "-1")
                {
                    entity.N_CUSTOMER_TYPE = null;
                }
                else
                {
                    entity.N_CUSTOMER_TYPE = Convert.ToInt32(FORM_CUSTOMER_TYPE.SelectedValue);
                }

                if (customerBLL.Update(entity))
                {
                    MessageBox.Show(this, "修改成功");
                }
                else
                {
                    MessageBox.Show(this, "修改失败");
                }
            }
            else
            {
                var entity = new Yx_Customer();
                var sql = "SELECT COUNT(*) FROM YX_CUSTOMER WHERE N_COMPANY_ID = " + FORM_COMPANY_ID.Value + " AND C_NAME = '" + FORM_NAME.Text.TrimEnd() + "'";
                if (customerBLL.GetCount(sql) > 0)
                {
                    MessageBox.Show(this, "此客户公司信息已添加！");
                    return;
                }
                entity.C_ADDRESS = FORM_ADDRESS.Text.TrimEnd();

                entity.C_NAME = FORM_NAME.Text.TrimEnd();
                entity.C_REAL_NAME = C_REAL_NAME.Text;
                entity.C_PHONE = FORM_PHONE.Text.Trim();
                entity.N_COMPANY_ID = FORM_COMPANY_ID.Value;
                entity.C_REMARK = FORM_REMARK.Text;
                entity.N_STATE = Convert.ToDecimal(FORM_STATE.SelectedValue);
                if (FORM_CUSTOMER_TYPE.SelectedValue == "-1")
                {
                    entity.N_CUSTOMER_TYPE = null;
                }
                else
                {
                    entity.N_CUSTOMER_TYPE = Convert.ToInt32(FORM_CUSTOMER_TYPE.SelectedValue);
                }
                if (customerBLL.Insert(entity))
                {
                    MessageBox.ShowAndRedirect(this, "新增成功", "/Modal/EditCustomer.aspx?IF_EDIT=FALSE");//&ID=" + entity.N_ID.ToString());
                }
                else
                {
                    MessageBox.Show(this, "新增失败");
                }
            }
        }

        protected override void UpdateTitle()
        {
            if (FORM_IFEDIT)
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"编辑客户信息\");");
            }
            else
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"添加客户信息\");");
            }
        }

        protected override void UpdateFormSize()
        {
            MessageBox.ResponseScript(this, "FORM_SIZE(880,420);");
        }
    }
}